
<%@page import="com.liferay.portal.kernel.json.JSONObject"%>
<%@page import="com.liferay.portal.kernel.json.JSONFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.json.JSONArray"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="qldc.pm.tiensu.service.CongViecBanGiaoLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.CongViecBanGiao"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qldc.pm.tiensu.dto.CongViecBanGiaoDTO"%>
<%@page import="qldc.pm.tiensu.service.BienBanBanGiaoHoSoLocalServiceUtil"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="qldc.pm.tiensu.model.BienBanBanGiaoHoSo"%>
<%@page import="com.liferay.portal.kernel.util.StringUtil"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>

<%@include file="../../init.jsp"%>

<%
SimpleDateFormat sdf = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);
String pathThemeImages = themeDisplay.getPathThemeImages();
String redirect = ParamUtil.getString(request, "redirect");

long bienBanBanGiaoHoSoId = ParamUtil.getLong(request, "bienBanBanGiaoHoSoId");

PortletURL redirectUrl = renderResponse.createRenderURL();
redirectUrl.setParameter("mvcPath", "/html/view.jsp");
redirectUrl.setParameter("tabs1", "danh_sach");
redirectUrl.setParameter("chonChucNangItem",TienSuConstants.LAP_BIEN_BAN_BAN_GIAO_HO_SO);

BienBanBanGiaoHoSo bienBan = null;
String mode = ParamUtil.getString(request, "mode");

boolean isDisplay = mode.equals("display");

String kinhGui = StringPool.BLANK;		
String nguoiLap = StringPool.BLANK;
String nguoiDuocBanGiao = StringPool.BLANK;
String chucVu = StringPool.BLANK;
String chucVuNguoiDuocBanGiao = StringPool.BLANK;
String noiDung = StringPool.BLANK;
String diaDiemBanGiao = StringPool.BLANK;
Date ngayLap = null;
Date ngayBanGiao = null;
List<CongViecBanGiao> congViecs = new ArrayList<CongViecBanGiao>();
JSONArray jsonResult = JSONFactoryUtil.createJSONArray();

if (bienBanBanGiaoHoSoId > 0) {
	bienBan = BienBanBanGiaoHoSoLocalServiceUtil.findBienBanBanGiaoHoSoId(bienBanBanGiaoHoSoId);
	
	if (Validator.isNotNull(bienBan)) {
		 kinhGui = bienBan.getKinhGui();		
		 nguoiLap = bienBan.getNguoiLap();
		 nguoiDuocBanGiao = bienBan.getNguoiDuocBanGiao();
		 chucVu = bienBan.getChucVu();
		 diaDiemBanGiao = bienBan.getDiaDiemBanGiao();
		 chucVuNguoiDuocBanGiao = bienBan.getChucVuNguoiDuocBanGiao();
		 noiDung = bienBan.getNoiDung();
		 ngayLap = bienBan.getNgayLap();
		 ngayBanGiao = bienBan.getNgayBanGiao();
		 congViecs = CongViecBanGiaoLocalServiceUtil.findByBienBanBanGiaoId(bienBanBanGiaoHoSoId);
		
		JSONObject obj;
		for (CongViecBanGiao dto:congViecs) {
			obj = JSONFactoryUtil.createJSONObject();				
			obj.put("tienDoThucHien", dto.getTienDoThucHien());
			obj.put("tenCongViec",dto.getTenCongViec());
			obj.put("chuThich", dto.getChuThich());

			jsonResult.put(obj);
		}
	}
}

%>

<portlet:actionURL var="lapBienBanBanGiaoURL">
	<portlet:param name="<%=TienSuConstants.ACTION_HANDLER_PARAM %>" value="<%=TienSuConstants.LAP_BIEN_BAN_BAN_GIAO_HO_SO %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="luuHoacInBienBan"/>
	<portlet:param name="redirect" value="<%=redirectUrl.toString() %>"/>
</portlet:actionURL>

<script type="text/javascript">
var congViecBanGiaosTable;

$(function() {
	var congViecBanGiaoss = [];
	congViecBanGiaoss = JSON.parse('<%=jsonResult.toString()%>');
	congViecBanGiaosTable = new BasicTableEditor(congViecBanGiaoss, "<portlet:namespace/>addGiayTo", {
		addOriginalRows : function(row, congViecBanGiaos, callback) {
			var tenCongViec = $("<textarea>").css('width','100%').attr({ type: "text" });		
			tenCongViec.val(congViecBanGiaos.tenCongViec);
			var tienDoThucHien = $("<input>").css('width','100%').attr({ type: "text" }).val(congViecBanGiaos.tienDoThucHien);
			var chuThich = $("<textarea>").css('width','100%').attr({ type: "text" }).val(congViecBanGiaos.chuThich);
			var tenCongViecTd = $("<td>").css('width','50%');
			var tienDoThucHienTd = $("<td>").css('width','25%');
			var chuThichTd = $("<td>").css('width','20%');

			tenCongViec.appendTo(tenCongViecTd.appendTo(row));
			tienDoThucHien.appendTo(tienDoThucHienTd.appendTo(row));
			chuThich.appendTo(chuThichTd.appendTo(row));
			<% 
			if (isDisplay) {
			%>
				tenCongViec.attr('readonly','readonly');
				tienDoThucHien.attr('readonly','readonly');
				chuThich.attr('readonly','readonly');
				$("<td>").appendTo(row);
			<%
			} else {
			%>
				var removeCongViecBanGiao = $("<img>").attr({ src: "<%=pathThemeImages %>/common/delete.png"});
				
				removeCongViecBanGiao.appendTo($("<td>").appendTo(row));
				
				removeCongViecBanGiao.click(function() {
					row.remove();
					row = null;
				});
			<%
			}
			%>
				
			callback(function() {
				var value = null;
				
				if(row != null) {
					value = {};
					
					value.tenCongViec = tenCongViec.val();
					value.tienDoThucHien = tienDoThucHien.val();
					value.chuThich = chuThich.val();
				}
				
				return value;
			});
				
			return row;
		},
		
		addEvent : function(row, callback) {
			var tenCongViec =  $("<textarea>").css('width','100%').attr({ type: "text"});
			var tienDoThucHien = $("<textarea>").css('width','100%').attr({ type: "text"});
			var chuThich =  $("<textarea>").css('width','100%').attr({ type: "text"});
			var removeCongViecBanGiao = $("<img>").attr({ src: "<%= pathThemeImages %>/common/delete.png"});
			
			var tenCongViecTd = $("<td>").css('width','50%');
			var tienDoThucHienTd = $("<td>").css('width','25%');
			var chuThichTd = $("<td>").css('width','20%');

			tenCongViec.appendTo(tenCongViecTd.appendTo(row));
			tienDoThucHien.appendTo(tienDoThucHienTd.appendTo(row));
			chuThich.appendTo(chuThichTd.appendTo(row));
			
			removeCongViecBanGiao.appendTo($("<td>").appendTo(row));
			
			removeCongViecBanGiao.click(function() {
				row.remove();
				row = null;
			});
					
			callback(function() {
				var value = null;
				
				if(row != null) {
					value = {};
					
					value.tenCongViec = tenCongViec.val();
					value.tienDoThucHien = tienDoThucHien.val();
					value.chuThich = chuThich.val();
				}
				
				return value;
			});
				
			return row;
		}
	}); 
	
	$("#<portlet:namespace/>ngayLap").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="ngay-lap" />",
			dateFormat : "dd/mm/yy"
			
		});
	$("#<portlet:namespace/>ngayBanGiao").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="ngay-ban-giao" />",
			dateFormat : "dd/mm/yy"
			
		});
	});


function <portlet:namespace/>validationForm(){
	var isOk = true;	
	
		var inputList = $(".mandatory");	
		console.log('inputList ',inputList)
		if (inputList.length > 0){
			for (var i = 0;i<inputList.length;i++){
				var firstItem = inputList.eq(i);
				if (firstItem.val() == ''){
					var messageName = firstItem.attr('message').toString();
					showPopupConfirm('<liferay-ui:message key="xin-vui-long-nhap-thong-tin" />  : ['+messageName+']',['Ok'],function(index){
						$(firstItem).focus();
						isOk = false;
						return;
					});
					isOk = false;
					break;
				}
			}	
		}
		
	
	return isOk;
	
}

function <portlet:namespace/>inBienBan(){
	console.log('congViecBanGiaosTable >> ',congViecBanGiaosTable.getValues());
	if (<portlet:namespace/>validationForm()){
		var congViecs = JSON.stringify(congViecBanGiaosTable.getValues())
		$("#<portlet:namespace/>fm").attr('target','_blank');
		$('#<portlet:namespace />cmd').val('<%=TienSuConstants.IN%>');
		$('#<portlet:namespace />congViecs').val(congViecs);
		$("#<portlet:namespace/>fm").submit(); 
	}
}; 

function <portlet:namespace/>luuBienBan(){
	$("#<portlet:namespace/>fm").removeAttr('target');
	if (<portlet:namespace/>validationForm()){
		var co = '<liferay-ui:message key="co" />';
	 	var khong = '<liferay-ui:message key="khong" />';
	 	var mess = '<liferay-ui:message key="ban-co-muon-luu-thong-tin" />';
			showPopupConfirm(mess,[co, khong], function(index) {
			    if (index == 0){
					var congViecs = JSON.stringify(congViecBanGiaosTable.getValues())
					$('#<portlet:namespace />congViecs').val(congViecs);

			    	$('#<portlet:namespace />cmd').val('<%=TienSuConstants.LUU%>');
					$("#<portlet:namespace/>fm").submit(); 
				} 
			});
	 	
		
	}
}; 
</script>

<form id="<portlet:namespace/>fm" method="POST" name="fm" action="<%=lapBienBanBanGiaoURL.toString() %>" target="_blank">
	 <input type="hidden" name="<portlet:namespace />cmd" id="<portlet:namespace />cmd" />
	 <input type="hidden" name="<portlet:namespace />bienBanBanGiaoHoSoId" value="<%=StringUtil.valueOf(bienBanBanGiaoHoSoId) %>" id="<portlet:namespace />bienBanBanGiaoHoSoId" />
	 <input type="hidden" name="<portlet:namespace />congViecs" value="" id="<portlet:namespace />congViecs" />
	<table class="gridnormal">
		<tr>
			<td class="aui-w20"><liferay-ui:message key="dia-diem-ban-giao" />&nbsp;<font color="red">*</font></td>
			<td class="aui-w30"><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" name="<portlet:namespace />diaDiemBanGiao" class="mandatory" value="<%=diaDiemBanGiao %>" message="<liferay-ui:message key='dia-diem-ban-giao' />" id="<portlet:namespace />diaDiemBanGiao" /></td>
			<td class="aui-w20"></td>
			<td class="aui-w30"></td>
		</tr>
		
		<tr>
			<td><liferay-ui:message key="nguoi-lap" />&nbsp;<font color="red">*</font></td>
			<td><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" name="<portlet:namespace />nguoiLap" class="mandatory" value="<%=nguoiLap %>" message="<liferay-ui:message key='nguoi-lap' />" id="<portlet:namespace />nguoiLap" /></td>
			<td><liferay-ui:message key="chuc-vu" />&nbsp;<font color="red">*</font></td>
			<td><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" name="<portlet:namespace />chucVu" class="mandatory"  value="<%=chucVu %>" message="<liferay-ui:message key='chuc-vu' />" id="<portlet:namespace />chucVu" /></td>
		</tr>
		
		<tr>
			<td><liferay-ui:message key="ban-giao-cho-ong-ba" />&nbsp;<font color="red">*</font></td>
			<td><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" name="<portlet:namespace />nguoiDuocBanGiao" class="mandatory" value="<%=nguoiDuocBanGiao %>" message="<liferay-ui:message key='ban-giao-cho-ong-ba' />" id="<portlet:namespace />nguoiDuocBanGiao" /></td>
			<td><liferay-ui:message key="chuc-vu" />&nbsp;<font color="red">*</font></td>
			<td><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" name="<portlet:namespace />chucVuNguoiDuocBanGiao" value="<%=chucVuNguoiDuocBanGiao %>" class="mandatory" message="<liferay-ui:message key='chuc-vu' />" id="<portlet:namespace />chucVuNguoiDuocBanGiao" /></td>
		</tr>
		
		<tr>
			<td><liferay-ui:message key="ngay-lap" />&nbsp;<font color="red">*</font></td>
			<td class='ip-datepicker'><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" value="<%=Validator.isNotNull(ngayLap)? sdf.format(ngayLap) :""%>" class="mandatory" message="<liferay-ui:message key='ngay-lap' />" style="width: 90%" name="<portlet:namespace />ngayLap" id="<portlet:namespace />ngayLap" /></td>
			<td><liferay-ui:message key="ngay-ban-giao" />&nbsp;<font color="red">*</font></td>
			<td class='ip-datepicker'><input <%=isDisplay ? "readonly='readonly'" : "" %> type="text" value="<%=Validator.isNotNull(ngayBanGiao)? sdf.format(ngayBanGiao) :""%>" class="mandatory" message="<liferay-ui:message key='ngay-ban-giao' />" style="width: 90%" name="<portlet:namespace />ngayBanGiao" id="<portlet:namespace />ngayBanGiao" /></td>
		</tr>
		
		<tr>
			<td><liferay-ui:message key="danh-sach-cong-viec-can-ban-giao" /></td>
			<td style="  padding-left: 0;padding-right: 0;" colspan="3">
				<table class="gridnormal">				
					<tr>
						<td>
							<table class="datagrid" style="width: 96%">
								<tr>
									<th class="aui-w50"><liferay-ui:message key="noi-dung-cong-viec" /></th>
									<th class="aui-w25"><liferay-ui:message key="tinh-hinh-thuc-hien" /></th>
									<th class="aui-w20"><liferay-ui:message key="chu-thich" /></th>
									<th style="width: 20px;" >
										<span id="<portlet:namespace/>addGiayTo">
										<%
										if (!isDisplay) {
										%>
										<liferay-ui:icon image="add"/>
										<%
										}
										%>
										</span>
									</th>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
					
		<tr>
			<td><liferay-ui:message key="ly-do-ban-giao" /></td>
			<td colspan="3">
				<textarea rows="3" <%=isDisplay ? "readonly='readonly'" : "" %> name="<portlet:namespace />noiDung"><%=noiDung %></textarea>
			</td>
		</tr>
					
		<tr>
			<td></td>
			<td colspan="3">
			<%
				if (!isDisplay) {
			%>
				<input type="button" class="btn btn-primary" value="<liferay-ui:message key="luu-bien-ban" />" onclick="<portlet:namespace/>luuBienBan();"/>
			<%
				}
			%>
				<input type="button" class="btn btn-primary" value="<liferay-ui:message key="in-bien-ban" />" onclick="<portlet:namespace/>inBienBan();"/>
				
				
				<input type="button" class="btn " value="<liferay-ui:message key="tro-ve" />"  onclick="javascript:submitForm(document.hrefFm,'<%=redirect %>');"/>
			
			</td>
		</tr>
	</table>
</form>
	